package com.erb.controller.zwj.admin;

import com.erb.constants.AdminInformConstants;
import com.erb.entity.InformDetailVo;
import com.erb.entity.InformVo;
import com.erb.entity.LoanUser;
import com.erb.entity.RespBean;
import com.erb.service.hzg.LoanUserService;
import com.erb.service.zwj.IInformService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/inform")
public class AdminInformController {
	@Resource
	private IInformService informService;
	@Resource
	private LoanUserService loanUserService;

	private DateFormat dateFormat = DateFormat.getDateTimeInstance();

	@GetMapping("/findInforms")
	public ModelAndView findInforms() {
		ModelAndView modelAndView = new ModelAndView("admin/inform/list");

		List<InformVo> informList = informService.findInformsByRecipientsId(AdminInformConstants.ADMIN_ID_LABEL);
		Map<Integer, String> dateMap = new HashMap<>(informList.size());
		for (InformVo informVo : informList) {
			if (informVo.getInformSenderName() == null && informVo.getInformSenderId() != null) {
				LoanUser loanUser = loanUserService.lulist(Integer.parseInt(informVo.getInformSenderId()));
				if(loanUser != null){
					informVo.setInformSenderName(loanUser.getName());
				}
			}
			dateMap.put(informVo.getInformId(), dateFormat.format(new Date(informVo.getSendTime())));
		}

		modelAndView.addObject("informList", informList);
		modelAndView.addObject("dateMap", dateMap);
		return modelAndView;
	}

	/**
	 * 查询前五条未读通知
	 *
	 * @param recipientsId 接收者ID
	 * @return
	 */
	@GetMapping("/findUnreadInforms")
	public RespBean findUnreadInforms(@RequestParam Integer recipientsId) {
		if (recipientsId == 0) {
			recipientsId = AdminInformConstants.ADMIN_ID_LABEL;
		}
		Map<String, Object> resultMap = new HashMap<>(2);

		List<InformVo> informVoList = informService.findInformVosForUnreadTop5(recipientsId);
		for (InformVo informVo : informVoList) {
			if (informVo.getInformSenderName() == null && informVo.getInformSenderId() != null) {
				LoanUser loanUser = loanUserService.lulist(Integer.parseInt(informVo.getInformSenderId()));
				if(loanUser != null){
					informVo.setInformSenderName(loanUser.getName());
				}
			}
		}

		// 未读消息，前五条
		resultMap.put("unreadInformList", informVoList);
		// 未读消息总数
		resultMap.put("unreadInformCount", informService.findCountUnread(recipientsId));

		return RespBean.ok("查询成功！", resultMap);
	}

	/**
	 * 读取通知详细
	 *
	 * @param informId
	 * @return
	 */
	@GetMapping("/readInformDetail")
	public RespBean readInformDetail(@RequestParam Integer informId) {
		InformDetailVo informDetail = informService.readInformDetail(informId);
		if (informDetail == null) {
			return RespBean.ok("消息不存在！");
		}

		return RespBean.ok("查询成功！", informDetail);
	}
}
